package com.example.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.entity.Orders;
import com.example.reggie.service.impl.OrderServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;
import javafx.scene.chart.PieChart;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        boolean submit = orderService.submit(orders);
        if (!submit){return R.error("下单失败");}
        return R.success("下单成功");
    }

    /**
     * 订单展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<IPage> userPage(int page, int pageSize){
        log.info("订单展示  page:{} pageSize:{} ",page,pageSize);
        Long userId = BaseContext.getCurrentId();
        IPage<Orders>iPage=new Page(page,pageSize);
        LambdaQueryWrapper<Orders>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(iPage,queryWrapper);

        List<Orders> records = iPage.getRecords();
        records.forEach((item)->{
            log.info("item订单展示:{}",item);
        });

        return R.success(iPage);
    }

    /**
     * 订单展示(后台)
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(int page, int pageSize, Long number, String beginTime,String endTime){
        log.info("page:{} pageSize:{} order_id:{} beginTime:{} endTime:{}",page,pageSize,number,beginTime,endTime);
        IPage<Orders>iPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Orders::getId,number);
        queryWrapper.gt(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.lt(endTime!=null,Orders::getOrderTime,endTime);
        orderService.page(iPage,queryWrapper);
        return R.success(iPage);
    }

    /**
     * 修改订单状态
     * @param
     * @return
     */
    @PutMapping()
    public R<String> updateStatus(@RequestBody Orders orders){
        log.info("想要修改的订单信息:{}",orders);
        if (!orderService.updateById(orders)){
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 再来一单
     * @param
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        log.info("再来一单 id:{}",orders.getId());
        if (!orderService.buyAgain(orders.getId())){
            return R.error("操作失败");
        }
        return R.success("操作成功");
    }

}